package com.source.lightfowl.basedata.controller;

import com.source.lightfowl.VO.IdCardVO;
import com.source.lightfowl.basedata.entity.IdCard;
import com.source.lightfowl.basedata.service.IdCardService;
import com.source.lightfowl.syslog.annotation.LogAnnotation;
import com.source.lightfowl.utils.MyResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Optional;

/**
 * @Author: 谢春泳
 * @Description:
 * @Date: 2020/1/10 13:19
 */
@Api(value = "id卡管理" ,tags = "id卡管理")
@RestController
@CrossOrigin
@RequestMapping
public class IdCardController {
    @Autowired
    private IdCardService idCardService;

    //查找全部
    @ApiOperation(value = "查询全部数据")
    @GetMapping("/idCard")
    public MyResult findAll(){

        List<IdCardVO> all = idCardService.findAll();
        if(all!=null)
            return MyResult.build(0,"成功",all);
        else
            return MyResult.build(1,"结果为空");
    }

    //查找 分页
    /*
        疑问：如果参数的页面数超过数据库的数据  要怎么显示？
     */
    @ApiOperation(value = "分页查询 两个参数 页数 个数/页")
    @GetMapping("/idCardFindAllPage")
    public MyResult findAllPage(int page,int pageNum){

        Page<IdCard> all = idCardService.findAllPage(page,pageNum);
        if(all!=null)
            return MyResult.build(0,"成功",all);
        else
            return MyResult.build(1,"结果为空");
    }

    //根据id查
    /*
        疑问：如果查找的id在数据库不存在要怎么编写逻辑？
     */
    @ApiOperation(value = "根据id查询数据")
    @GetMapping("/idCard/id")
    public MyResult findById(Integer id){
        Optional<IdCard> house = idCardService.findById(id);
        if(house!=null)
            return MyResult.build(0,"成功",house);
        else
            return MyResult.build(1,"结果为空");
    }

    //增加 修改
    @ApiOperation(value = "增加/修改数据 ")
    @PostMapping("/idCard")
    @LogAnnotation(operEvent = "操作类型：添加/修改",operType = 2)
    public MyResult create(IdCard idCard, HttpServletRequest request){

        try {
            idCardService.save(idCard,request);
            return MyResult.build(0,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return MyResult.build(1, "操作失败");
        }

    }

    //删除
    /*
        疑问：如果删除的id在数据库不存在要怎么编写逻辑？
     */
    @ApiOperation(value = "根据id删除数据")
    @DeleteMapping("/idCard/id")
    @LogAnnotation(operEvent = "操作类型：删除记录",operType = 3)
    public MyResult deleteById(Integer id){

        try{
            idCardService.deleteById(id);
            return MyResult.build(0,"操作成功");
        }catch (Exception e){
            e.printStackTrace();
            return MyResult.build(1,"删除操作出现异常");
        }
    }



    //删除 批量
    @ApiOperation(value = "批量删除数据：参数传递id的字符串 用英文的逗号分开")
    @DeleteMapping("/idCardDeleteBatch")
    @LogAnnotation(operEvent = "操作类型：批量删除记录",operType = 4)
    public MyResult deleteBatch(String ids){    //注意：参数的分隔符逗号是英文的


        idCardService.deleteBatch(ids);

        return MyResult.build(0,"看是否少了删除的id");

    }
}
